﻿/*
 * This class was originally implemented by Josh Smith and modified by
 * Laurent Bugnion.
 * 
 * */

using System;
using System.Diagnostics;
using System.Windows.Input;

namespace Sponsorship.Ui.Wpf.Commanding
{
    public class RelayCommand : ICommand
    {
        private readonly Predicate<object> _canExecute;
        private readonly Action<object> _execute;

        public RelayCommand (Action<object> execute)
            : this (execute, null)
        {
        }

        public RelayCommand (Action<object> execute, Predicate<object> canExecute)
        {
            if (execute == null)
                throw new ArgumentNullException ("execute");

            _execute = execute;
            _canExecute = canExecute;
        }

        [DebuggerStepThrough]
        public bool CanExecute (object parameter)
        {
            return _canExecute == null || _canExecute (parameter);
        }

        public void Execute (object parameter)
        {
            _execute (parameter);
        }

        public event EventHandler CanExecuteChanged
        {
            add { CommandManager.RequerySuggested += value; }
            remove { CommandManager.RequerySuggested -= value; }
        }
    }
}